package com.bdh.javaBasicStudy;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;

/**
 * @projectName: maven_project
 * @package: com.bdh.javaBasicStudy
 * @className: FileUtilsExer
 * @author: Dream
 * @description: TODO
 * @date: 2022/6/20 19:24
 */
public class FileUtilsExer {
    //读文件
    @Test
    public void FileOperaExer() throws IOException {
        ArrayList<String> lines = new ArrayList<>();
        lines.add("第一行");
        lines.add("第二行");
//        FileUtils.writeLines(new File("/Users/dreamyouth/Downloads/DataTest/hello.txt"), lines, true);
//        FileUtils.write(new File("/Users/dreamyouth/Downloads/DataTest/hello.txt"), "woshdingyi", true);
        //同行输入
        FileUtils.writeStringToFile(new File("/Users/dreamyouth/Downloads/DataTest/hello.txt"), "nihao", "UTF-8", true);
    }

    //写文件
    @Test
    public void FileReadExer() throws IOException {
        System.out.println(FileUtils.readFileToString(new File("/Users/dreamyouth/Downloads/DataTest/hello.txt")));
        System.out.println(FileUtils.readLines(new File("/Users/dreamyouth/Downloads/DataTest/hello.txt")));
    }
    //快速下载
    @Test
    public void QuickDownloadExer() throws IOException {
        //Method 1
        URL url = new URL("http://www.baidu.com/img/baidu_logo.gif");
        File file = new File("/Users/dreamyouth/Downloads/DataTest/baidu1.gif");
        FileUtils.copyURLToFile(url, file);

        //Method 2
        InputStream in = new URL("http://www.baidu.com/img/baidu_logo.gif").openStream();
        byte[] gif = IOUtils.toByteArray(in);
        FileUtils.writeByteArrayToFile(new File("/Users/dreamyouth/Downloads/DataTest/baidu2.gif"), gif);

        //Method3
        InputStream in3 = new URL("http://www.baidu.com/img/baidu_logo.gif").openStream();
        byte[] gif3 = IOUtils.toByteArray(in3);
        IOUtils.write(gif3, new FileOutputStream(new File("/Users/dreamyouth/Downloads/DataTest/baidu3.gif")));
        IOUtils.closeQuietly(in3);
    }

    //快速复制
    @Test
    public void QuickCopyExer() throws IOException {
        //Method1
        FileUtils.copyFile(new File("Users/dreamyouth/Downloads/DataTest/hello.txt"), new File("Users/dreamyouth/Downloads/DataTest/hello2.txt"));

        //Method2
        Writer write = new FileWriter("Users/dreamyouth/Downloads/DataTest/hello.txt");
        InputStream ins = new FileInputStream(new File("Users/dreamyouth/Downloads/DataTest/hello2.txt"));
        IOUtils.copy(ins, write);
        write.close();
        IOUtils.closeQuietly(ins);
        //Metho3 同时写入两个不同文件 略
    }
    //获取网页的内容
    @Test
    public void GetWebContents() throws IOException {
        URL webUrl = new URL("https://wenku.baidu.com/view/7999faba960590c69ec376be.html?from=search");
        InputStream ins = webUrl.openStream();
        String contents = IOUtils.toString(ins, "gb2312");
        System.out.println("contents is" + contents);
    }
    //其余的一些常用操作
    @Test
    public void getDirTest(){
        //获取基本信息
        //获取临时目录 java.io.tmpdir.getUserDirectoryPath返回路径字符串
        System.out.println(FileUtils.getTempDirectory());
        //获取用户主目录 user.home.getUserDirectoryPath返回路径字符串
        System.out.println(FileUtils.getUserDirectory());

        //以可读的方式，返回文件的大小EB, PB, TB, GB, MB, KB or bytes
        System.out.println(FileUtils.byteCountToDisplaySize(10000000));
        System.out.println(FileUtils.byteCountToDisplaySize(1));
    }
    @Test
    public void FileOperation() throws IOException {
        //创建文件，如果文件存在则更新时间；如果不存在，创建一个空文件
        //创建空文件的方式为：
        FileUtils.touch(new File("/Users/dreamyouth/Downloads/DataTest/test4.txt"));

        //文件内容对比
        FileUtils.contentEquals(new File("/Users/dreamyouth/Downloads/DataTest/test4.txt"), new File("/Users/dreamyouth/Downloads/DataTest/hello.txt"));
        //忽略换行符的对比
        FileUtils.contentEqualsIgnoreEOL(new File("/Users/dreamyouth/Downloads/DataTest/test4.txt"), new File("/Users/dreamyouth/Downloads/DataTest/hello.txt"), null);

        FileUtils.toFile(new URL("/Users/dreamyouth/Downloads/DataTest/test4.txt"));
        FileUtils.writeStringToFile(new File("/Users/dreamyouth/Downloads/DataTest/test4.txt"),"string","utf-8",true);
    }

}
